%%%% Peak-to-Trough Changes - Avg. All Recessions - (10/7/2024)
%% From "The Impact of UI and Unsecured Credit on Business Cycles"
%% Include acyclical credit experiment

%%%% Load the Results %%%%
%% Zsim: exogenous state of economy.  variable 1 in 'exogenous_fluctuations' file
%% Csim_1: Credit, UI Extensions (benchmark)
%% Csim_5: Credit, Acyclical UI
%% Csim_6: Credit, UI Level Increase
%% Csim_7: No Credit, UI Extension
%% Csim_8: No Credit, Acyclical UI
%% Csim_9: No Credit, UI Level Increase
%% Csim_10: Acyclical Credit, UI Extensions
%% Csim_11: Acyclical Credit, Acyclical UI
%% Dsim_1: Credit, UI Extension (benchmark)
%% Dsim_5: Credit, Acyclical UI
%% Dsim_6: Credit, UI Level Increase
%% Dsim_10: Acyclical Credit, UI Extensions
%% Dsim_11: Acyclical Credit, Acyclical UI

%%%% Log Data %%%%
ln_C1 = log(Csim_1);
ln_C5 = log(Csim_5);
ln_C6 = log(Csim_6);
ln_C7 = log(Csim_7);
ln_C8 = log(Csim_8);
ln_C9 = log(Csim_9);
ln_C10 = log(Csim_10);
ln_C11 = log(Csim_11);
ln_D1 = log(-Dsim_1);
ln_D5 = log(-Dsim_5);
ln_D6 = log(-Dsim_6);
ln_D10 = log(-Dsim_10);
ln_D11 = log(-Dsim_11);

%%%% HP Filtered Data %%%%
HP_C1 = HP_Filter(ln_C1,1600);
HP_C5 = HP_Filter(ln_C5,1600);
HP_C6 = HP_Filter(ln_C6,1600);
HP_C7 = HP_Filter(ln_C7,1600);
HP_C8 = HP_Filter(ln_C8,1600);
HP_C9 = HP_Filter(ln_C9,1600);
HP_C10 = HP_Filter(ln_C10,1600);
HP_C11 = HP_Filter(ln_C11,1600);
HP_D1 = HP_Filter(ln_D1,1600);
HP_D5 = HP_Filter(ln_D5,1600);
HP_D6 = HP_Filter(ln_D6,1600);
HP_D10 = HP_Filter(ln_D10,1600);
HP_D11 = HP_Filter(ln_D11,1600);

%%%% Calculating Aggregate Fluctuations %%%%
PT_HP1 = zeros(100,1);
PT_HP5 = zeros(100,1);
PT_HP6 = zeros(100,1);
PT_HP7 = zeros(100,1);
PT_HP8 = zeros(100,1);
PT_HP9 = zeros(100,1);
PT_HP10 = zeros(100,1);
PT_HP11 = zeros(100,1);
PT_D1 = zeros(100,1);
PT_D5 = zeros(100,1);
PT_D6 = zeros(100,1);
PT_D10 = zeros(100,1);
PT_D11 = zeros(100,1);
TD_HP1 = zeros(100,1);
TD_HP5 = zeros(100,1);
TD_HP6 = zeros(100,1);
TD_HP7 = zeros(100,1);
TD_HP8 = zeros(100,1);
TD_HP9 = zeros(100,1);
TD_HP10 = zeros(100,1);
TD_HP11 = zeros(100,1);
TD_HP_D1 = zeros(100,1);
TD_HP_D5 = zeros(100,1);
TD_HP_D6 = zeros(100,1);
TD_HP_D10 = zeros(100,1);
TD_HP_D11 = zeros(100,1);
count_recession = 0;
switch_recession = 0;
for t = 302:2300
	if Zsim(t) < 1.0 & Zsim(t-1) == 1.0
		peak_1 = HP_C1(t-1);
		peak_5 = HP_C5(t-1);
		peak_6 = HP_C6(t-1);
		peak_7 = HP_C7(t-1);
        peak_8 = HP_C8(t-1);
        peak_9 = HP_C9(t-1);
        peak_10 = HP_C10(t-1);
        peak_11 = HP_C11(t-1);
        peak_D1 = HP_D1(t-1);
        peak_D5 = HP_D5(t-1);
        peak_D6 = HP_D6(t-1);
        peak_D10 = HP_D10(t-1);
        peak_D11 = HP_D11(t-1);
		trough_1 = HP_C1(t);
		trough_5 = HP_C5(t);
		trough_6 = HP_C6(t);
		trough_7 = HP_C7(t);
        trough_8 = HP_C8(t);
        trough_9 = HP_C9(t);
        trough_10 = HP_C10(t);
        trough_11 = HP_C11(t);
        trough_D1 = HP_D1(t);
        trough_D5 = HP_D5(t);
        trough_D6 = HP_D6(t);
        trough_D10 = HP_D10(t);
        trough_D11 = HP_D11(t);
		TD_1 = 0.0;
		TD_5 = 0.0;
		TD_6 = 0.0;
		TD_7 = 0.0;
        TD_8 = 0.0;
        TD_9 = 0.0;
        TD_10 = 0.0;
        TD_11 = 0.0;
        TD_D1 = 0.0;
        TD_D5 = 0.0;
        TD_D6 = 0.0;
        TD_D10 = 0.0;
        TD_D11 = 0.0;
		count_recession = count_recession + 1;
		switch_recession = 1;
	end

	if Zsim(t) == 1.0 & Zsim(t-1) < 1.0
		switch_recession = 0;
		PT_HP1(count_recession) = peak_1 - trough_1;
		PT_HP5(count_recession) = peak_5 - trough_5;
		PT_HP6(count_recession) = peak_6 - trough_6;
		PT_HP7(count_recession) = peak_7 - trough_7;
        PT_HP8(count_recession) = peak_8 - trough_8;
        PT_HP9(count_recession) = peak_9 - trough_9;
        PT_HP10(count_recession) = peak_10 - trough_10;
        PT_HP11(count_recession) = peak_11 - trough_11;
        PT_D1(count_recession) = peak_D1 - trough_D1;
        PT_D5(count_recession) = peak_D5 - trough_D5;
        PT_D6(count_recession) = peak_D6 - trough_D6;
        PT_D10(count_recession) = peak_D10 - trough_D10;
        PT_D11(count_recession) = peak_D11 - trough_D11;
		TD_HP1(count_recession) = TD_1;
		TD_HP5(count_recession) = TD_5;
		TD_HP6(count_recession) = TD_6;
		TD_HP7(count_recession) = TD_7;
        TD_HP8(count_recession) = TD_8;
        TD_HP9(count_recession) = TD_9;
        TD_HP10(count_recession) = TD_10;
        TD_HP11(count_recession) = TD_11;
        TD_HP_D1(count_recession) = TD_D1;
        TD_HP_D5(count_recession) = TD_D5;
        TD_HP_D6(count_recession) = TD_D6;
        TD_HP_D10(count_recession) = TD_D10;
        TD_HP_D11(count_recession) = TD_D11;
	end

	if switch_recession == 1
		if HP_C1(t) < trough_1
			trough_1 = HP_C1(t);
		end
		if HP_C5(t) < trough_5
			trough_5 = HP_C5(t);
		end
		if HP_C6(t) < trough_6
			trough_6 = HP_C6(t);
		end
		if HP_C7(t) < trough_7
			trough_7 = HP_C7(t);
        end
        if HP_C8(t) < trough_8
			trough_8 = HP_C8(t);
        end
        if HP_C9(t) < trough_9
			trough_9 = HP_C9(t);
        end
        if HP_C10(t) < trough_10
			trough_10 = HP_C10(t);
        end
        if HP_C11(t) < trough_11
			trough_11 = HP_C11(t);
        end
        if HP_D1(t) < trough_D1
			trough_D1 = HP_D1(t);
        end
        if HP_D5(t) < trough_D5
			trough_D5 = HP_D5(t);
        end
        if HP_D6(t) < trough_D6
			trough_D6 = HP_D6(t);
        end
        if HP_D10(t) < trough_D10
			trough_D10 = HP_D10(t);
        end
        if HP_D11(t) < trough_D11
			trough_D11 = HP_D11(t);
		end

		TD_1 = TD_1 + peak_1 - HP_C1(t);
		TD_5 = TD_5 + peak_5 - HP_C5(t);
		TD_6 = TD_6 + peak_6 - HP_C6(t);
		TD_7 = TD_7 + peak_7 - HP_C7(t);
        TD_8 = TD_8 + peak_8 - HP_C8(t);
        TD_9 = TD_9 + peak_9 - HP_C9(t);
        TD_10 = TD_10 + peak_10 - HP_C10(t);
        TD_11 = TD_11 + peak_11 - HP_C11(t);
        TD_D1 = TD_D1 + peak_D1 - HP_D1(t);
        TD_D5 = TD_D5 + peak_D5 - HP_D5(t);
        TD_D6 = TD_D6 + peak_D6 - HP_D6(t);
        TD_D10 = TD_D10 + peak_D10 - HP_D10(t);
        TD_D11 = TD_D11 + peak_D11 - HP_D11(t);

	end

end
avg_PT_HP1 = sum(PT_HP1(1:count_recession))/count_recession;
avg_PT_HP5 = sum(PT_HP5(1:count_recession))/count_recession;
avg_PT_HP6 = sum(PT_HP6(1:count_recession))/count_recession;
avg_PT_HP7 = sum(PT_HP7(1:count_recession))/count_recession;
avg_PT_HP8 = sum(PT_HP8(1:count_recession))/count_recession;
avg_PT_HP9 = sum(PT_HP9(1:count_recession))/count_recession;
avg_PT_HP10 = sum(PT_HP10(1:count_recession))/count_recession;
avg_PT_HP11 = sum(PT_HP11(1:count_recession))/count_recession;
avg_PT_D1 = sum(PT_D1(1:count_recession))/count_recession;
avg_PT_D5 = sum(PT_D5(1:count_recession))/count_recession;
avg_PT_D6 = sum(PT_D6(1:count_recession))/count_recession;
avg_PT_D10 = sum(PT_D10(1:count_recession))/count_recession;
avg_PT_D11 = sum(PT_D11(1:count_recession))/count_recession;
disp('')
disp('---------------------------------------')
disp('--- Avg Peak-to-Trough, Consumption ---')
fprintf('   (1)  Credit, UI Ext       : %8.6f \n', avg_PT_HP1);
fprintf('   (5)  Credit, Acyc UI      : %8.6f \n', avg_PT_HP5);
fprintf('   (6)  Credit, Inc Lev      : %8.6f \n', avg_PT_HP6);
fprintf('   (7)  No Credit, UI Ext    : %8.6f \n', avg_PT_HP7);
fprintf('   (8)  No Credit, Acyc UI   : %8.6f \n', avg_PT_HP8);
fprintf('   (9)  No Credit, Inc Lev   : %8.6f \n', avg_PT_HP9);
fprintf('   (10) Acyc Credit, UI Ext  : %8.6f \n', avg_PT_HP10);
fprintf('   (11) Acyc Credit, Acyc UI : %8.6f \n', avg_PT_HP11);

disp('')
disp('')
disp('---------------------------------------')
disp('------ Avg Peak-to-Trough, Credit -----')
fprintf('   (1)  Credit, UI Ext      : %8.6f \n', avg_PT_D1);
fprintf('   (5)  Credit, Acyc UI     : %8.6f \n', avg_PT_D5);
fprintf('   (6)  Credit, Inc Lev     : %8.6f \n', avg_PT_D6);
fprintf('   (10) Acyc Credit, UI Ext : %8.6f \n', avg_PT_D10);
fprintf('   (11) Acyc Credit, Acyc UI: %8.6f \n', avg_PT_D11);

avg_TD_HP1 = sum(TD_HP1(1:count_recession))/count_recession;
avg_TD_HP5 = sum(TD_HP5(1:count_recession))/count_recession;
avg_TD_HP6 = sum(TD_HP6(1:count_recession))/count_recession;
avg_TD_HP7 = sum(TD_HP7(1:count_recession))/count_recession;
avg_TD_HP8 = sum(TD_HP8(1:count_recession))/count_recession;
avg_TD_HP9 = sum(TD_HP9(1:count_recession))/count_recession;
avg_TD_HP10 = sum(TD_HP10(1:count_recession))/count_recession;
avg_TD_HP11 = sum(TD_HP11(1:count_recession))/count_recession;
disp('')
disp('')
disp('----------------------------------------')
disp('--- Avg Total Deviation, Consumption ---')
fprintf('   (1)  Credit, UI Ext      : %8.6f \n', avg_TD_HP1);
fprintf('   (5)  Credit, Acyc UI     : %8.6f \n', avg_TD_HP5);
fprintf('   (6)  Credit, Inc Lev     : %8.6f \n', avg_TD_HP6);
fprintf('   (7)  No Credit, UI Ext   : %8.6f \n', avg_TD_HP7);
fprintf('   (8)  No Credit, Acyc UI  : %8.6f \n', avg_TD_HP8);
fprintf('   (9)  No Credit, Inc Lev  : %8.6f \n', avg_TD_HP9);
fprintf('   (10) Acyc Credit, UI Ext : %8.6f \n', avg_TD_HP10);
fprintf('   (11) Acyc Credit, Acyc UI: %8.6f \n', avg_TD_HP11);

avg_TD_D1 = sum(TD_HP_D1(1:count_recession))/count_recession;
avg_TD_D5 = sum(TD_HP_D5(1:count_recession))/count_recession;
avg_TD_D6 = sum(TD_HP_D6(1:count_recession))/count_recession;
avg_TD_D10 = sum(TD_HP_D10(1:count_recession))/count_recession;
avg_TD_D11 = sum(TD_HP_D11(1:count_recession))/count_recession;
disp('')
disp('')
disp('----------------------------------------')
disp('------ Avg Total Deviation, Credit -----')
fprintf('   (1)  Credit, UI Ext      : %8.6f \n', avg_TD_D1);
fprintf('   (5)  Credit, Acyc UI     : %8.6f \n', avg_TD_D5);
fprintf('   (6)  Credit, Inc Lev     : %8.6f \n', avg_TD_D6);
fprintf('   (10) Acyc Credit, UI Ext : %8.6f \n', avg_TD_D10);
fprintf('   (11) Acyc Credit, Acyc UI: %8.6f \n', avg_TD_D11);